from httprunner.api import HttpRunner
from httprunner import report
from common.Operation_csv import Op_csv
from common.Operation_file import rename
import  os
# import time
# currentTime = time.strftime("%Y%m%d%H%M%S", time.localtime())
runner = HttpRunner(
    log_level="INFO",
    # log_level="debug",
    log_file="test_log/abobo.log"
)

#哈哈
#runner.run("docs/data/demo-quickstart-2.yml")
#呵呵
#runner.run("docs/data/")
#
#runner.run(["docs/data/", "files/demo-quickstart-2.yml"])

summary = runner.run("testcases/用户端.yml")
# print(summary)
# print(summary)
report_path = report.gen_html_report(
    summary,
    # report_template="D:/my_httprunner/venv/Lib/site-packages/httprunner/static/extent-theme-template.html",
    # report_dir="reports",
    # report_template="my_report_template/extent-theme-template.html",
    report_dir="innisfree_autotest/templates/reports/peoples",          #报告的生成路径
)


#按时间重命令报告名称
# reports_path = os.path.abspath(os.path.dirname(__file__))+"/reports"
reports_path = os.path.abspath(os.path.dirname(__file__))+"/innisfree_autotest/templates/reports/peoples"
rename(reports_path,"测试环境")

#统计测试结果写入CSV文件。
abobo = Op_csv()
initValue=abobo.initValue
# #读取执行结果
# records = summary["details"][0]["records"]
records = summary["details"]
for j in records:
    for i in j["records"]:
# for i in records:
        meta_datas = i['meta_datas']
        initValue['用例ID'] = i['name']
        initValue['执行结果'] = i['status']
        initValue['错误信息'] = i['attachment']
        if meta_datas != None:
            # initValue['用例ID'] = i['name']
            # initValue['执行结果'] = i['status']
            # initValue['错误信息'] = i['attachment']
            initValue['响应时长(ms)'] = i['response_time']
            initValue['请求方式'] = meta_datas['data'][0]['request']['method']
            initValue['请求头'] = meta_datas['data'][0]['request']['headers']
            initValue['请求url'] = meta_datas['data'][0]['request']['url']
            try:
                temp_data = meta_datas['data'][0]['request']['body']
                initValue['请求data'] = str(temp_data).replace('&#34;','')
            except KeyError :
                initValue['请求data'] = ''
            try:
                initValue['响应码'] = meta_datas['data'][0]['response']['status_code']
                initValue['响应体'] = meta_datas['data'][0]['response']['json']
            except Exception as e:
                initValue['响应码'] = ''
                initValue['响应体'] = ''
        abobo.writerow_csv(initValue)
        for tempKey in initValue.keys():
            initValue[tempKey] = ''






